package com.neusoft.nep.NEPS.controller;

import com.neusoft.tools.*;
import com.neusoft.nep.entity.User;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import java.io.IOException;
import java.util.Map;

import static com.neusoft.tools.FXMLUtils.switchToShow;


public class UserRegisterViewController {
    @FXML
    private Button btn_back;
    @FXML
    private Button btn_register;
    @FXML
    private TextField txt_phoneNumber;
    @FXML
    private TextField txt_userName;
    @FXML
    private TextField txt_password;
    @FXML
    private TextField txt_dateOfBirth;
    @FXML
    private RadioButton maleRadio;
    @FXML
    private RadioButton femaleRadio;
    @FXML
    private TextField txt_confirmedPassword;

    private ToggleGroup toggleGroup;

    @FXML
    public void initialize() {
        toggleGroup = new ToggleGroup();
        maleRadio.setToggleGroup(toggleGroup);
        femaleRadio.setToggleGroup(toggleGroup);
    }

    /**
     * 回退到登陆界面
     */
    public void back() throws IOException {
        switchToShow(btn_back, "NEPS/view/UserLoginView.fxml");
    }

    /**
     * 存储注册的用户信息
     */
    public void saveUser() throws IOException {
        //  密码中不允许空格
        String phoneNumber = txt_phoneNumber.getText().trim();
        String userName = txt_userName.getText().trim();
        String password = txt_password.getText();
        String confirmedPassword = txt_confirmedPassword.getText();
        String dateOfBirth = txt_dateOfBirth.getText().trim();

        //  一些输入格式检测
        if (ValidationUtils.isNullOrBlank(phoneNumber) ||
                ValidationUtils.isNullOrBlank(userName) ||
                ValidationUtils.isNullOrBlank(password) ||
                ValidationUtils.isNullOrBlank(confirmedPassword) ||
                ValidationUtils.isNullOrBlank(dateOfBirth)) {
            AlertUtils.showDialog("错误", "请填写所有必填字段。");
            return;
        }
        if(ValidationUtils.containBlank(password) || ValidationUtils.containBlank(confirmedPassword)){
            AlertUtils.showDialog("错误", "密码中不能包含空格。");
            return;
        }
        if (!ValidationUtils.isYYYYMMDD(dateOfBirth)) {
            AlertUtils.showDialog("错误", "出生日期格式不正确，请输入有效格式日期。");
            return;
        }
        if (ValidationUtils.containsChinese(password)) {
            AlertUtils.showDialog("错误", "密码中不能包含中文字符。");
            return;
        }
        if (!ValidationUtils.isDigitOrHyphen(phoneNumber)) {
            AlertUtils.showDialog("错误", "手机号格式不正确，只能包含数字或连字符(-)。");
            return;
        }
        if (!ValidationUtils.isChineseOrLetter(userName)) {
            AlertUtils.showDialog("错误", "姓名格式不正确，只能包含中文或英文字母。");
            return;
        }
        if (!password.equals(confirmedPassword)) {
            AlertUtils.showDialog("错误", "两次输入的密码不一致。");
            return;
        }
        RadioButton selectedRadio = (RadioButton) toggleGroup.getSelectedToggle();
        if (selectedRadio == null) {
            AlertUtils.showDialog("错误", "请选择性别。");
            return;
        }
        String gender = selectedRadio.getText();
        User newUser = new User(dateOfBirth, gender, password, phoneNumber, userName);
        Map<String, User> userMap = FileUtils.readMapObject("user.txt");

        //  检测用户是否已存在
        if (userMap.containsKey(newUser.getPhoneNumber())){
            AlertUtils.showDialog("错误", "用户已存在，请登录。");
            back();
        }
        else {
            userMap.put(newUser.getPhoneNumber(), newUser);
            FileUtils.writeMapObject("user.txt", userMap);
            AlertUtils.showDialog(null, "注册成功！");
            FXMLUtils.switchToShow(btn_register, "NEPS/view/UserLoginView.fxml");
            back();
        }
    }
}
